/*
 * Copyright © 2021-now 尛飛俠（Denvie） All rights reserved.
 */

package cn.denvie.springboot.dds.controller;

import cn.denvie.springboot.dds.config.MyRoutingDataSource;
import cn.denvie.springboot.dds.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户控制器。
 *
 * @author Denvie
 * @date 2022/3/28
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
@ConditionalOnProperty(name = "spring.profiles.active", havingValue = "jdbc")
public class JdbcController {
    @Autowired
    private MyRoutingDataSource myRoutingDataSource;
    @Autowired
    private UserService userService;

    @RequestMapping("/query")
    public Object query(@RequestParam(value = "ds", defaultValue = "ds1") String dsName) {
        // 通过前端参数动态改变数据源
        myRoutingDataSource.getDsName().set(dsName);

        return userService.list();
    }
}
